package com.library.dcy.dao;


import com.library.dcy.entity.Library;
import com.library.dcy.util.BaseDao;
import com.library.dcy.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class LibraryDaoImpl extends BaseDao implements libraryDao {
    String path="resources/mybatis-config.xml";
    /**
     * 查询总记录条数方法
     *
     * @return
     */
    @Override
    public int totalCount() throws Exception {
        SqlSession session = MybatisUtil.getSession(path);
        libraryDao mapper = session.getMapper(libraryDao.class);
//        String sql = "SELECT COUNT(1) FROM reader";
//        ResultSet sel = set(sql);
//        int count = 0;
//        try {
//            while (sel.next()) {
//                count = sel.getInt(1);
//                }
//            } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
        return mapper.totalCount();
    }
    /**
     * 查询
     *
     * @return
     */
    @Override
    public List<Library> list(Map map) throws Exception {
//        List list = new ArrayList();
        SqlSession session = MybatisUtil.getSession(path);
        libraryDao mapper = session.getMapper(libraryDao.class);
        List<Library> list = mapper.list(map);
//        String sql = "SELECT reaId,idCard,pwd,NAME,age,phone,price FROM reader WHERE NAME LIKE CONCAT('%',?,'%') LIMIT ?,?";
//        ResultSet sel = set(sql,name,start, pageSize);
//        try {
//            while (sel.next()) {
//                int reaId = sel.getInt(1);
//                String idCard = sel.getString(2);
//                String pwd = sel.getString(3);
//                String Name = sel.getString(4);
//                int age = sel.getInt(5);
//                String phone = sel.getString(6);
//                int price = sel.getInt(7);
//                Library library = new Library(reaId, idCard, pwd, Name, age, phone, price);
//                list.add(library);
//                }
//            } catch (SQLException throwables) {
//            throwables.printStackTrace();
//        }
                return list;
    }

    /**
     * 注销方法
     *
     * @return
     */
    @Override
    public int cancel(Integer reaId) {
        SqlSession session = null;
        try {
            session = MybatisUtil.getSession(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        libraryDao mapper = session.getMapper(libraryDao.class);
        return mapper.cancel(reaId);
    }
    /**
     * 补办
//     * @param reaId
//     * @param idCard
//     * @param pwd
//     * @param name
//     * @param age
//     * @param phone
//     * @param price
     * @return
     */
    @Override
    public int replace(Library library) throws Exception {
        SqlSession session = MybatisUtil.getSession(path);
        libraryDao mapper = session.getMapper(libraryDao.class);
        return mapper.replace(library);
    }
    /**
     * 充值
//     * @param id
//     * @param newprice
     * @return
     */
    @Override
    public int chongzhi(Library library) {
        SqlSession session = null;
        try {
            session = MybatisUtil.getSession(path);
        } catch (Exception e) {
            e.printStackTrace();
        }
        libraryDao mapper = session.getMapper(libraryDao.class);
        return mapper.chongzhi(library);
    }
}
